package com.bailian.spring.servelt.demo.service.impl;

import com.bailian.spring.servelt.demo.BeanFactory;
import com.bailian.spring.servelt.demo.dao.DemoDao;
import com.bailian.spring.servelt.demo.dao.impl.DemoDaoImpl;
import com.bailian.spring.servelt.demo.service.DemoService;

import java.util.List;

/**
 * @author gaoxu
 * date: Created on 2021/10/28 下午2:50
 */
public class DemoServiceImpl implements DemoService {

    /**
     * 方式0:强耦合的方式进行创建bean
     */
    private DemoDao demoDao0 = new DemoDaoImpl();

    /**
     * 优化1:依赖静态工厂返回,之后无论dao如何实现,只要修改静态工厂的代码就可以实现
     */
    DemoDao demoDao1 = BeanFactory.getDemoDao();

    /**
     * 优化2:依赖静态工厂,但是从配置文件中读取需要实例化的类,通过反射进行实例化操作;
     */
    DemoDao demoDao2 = (DemoDao) BeanFactory.getDemoDaoByProperties("demoDao");

    /**
     * 优化3:依赖缓存+配置文件+反射,实现bean获取
     *
     * @return
     */
    DemoDao demoDao3 = (DemoDao) BeanFactory.getBeanByPropertiesAndMap("demoDao");

    public DemoServiceImpl() {
        for (int i = 0; i < 10; i++) {
            Object demoDao = BeanFactory.getBeanByPropertiesAndMap("demoDao");
            System.out.println(demoDao);
        }
    }

    @Override
    public List<String> findAll() {
        return demoDao3.findAll();
    }
}
